package com.baize.motionlayoutdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.transition.TransitionManager

/**
 * 过渡动画三：TransitionManager#beginDelayedTransition + ConstraintSet 通过这种方式，并不会清空控件
 * 与TransitionManager#go方法相比，这种方式只能用于ConstraintLayout
 */
class ConstraintActivity : AppCompatActivity(),View.OnClickListener {

  private var toggle = true

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_constraint_start)
    findViewById<ImageView>(R.id.iv_image).setOnClickListener(this)
  }

  override fun onClick(v: View?) {
    val root = findViewById<ConstraintLayout>(R.id.root)
    TransitionManager.beginDelayedTransition(root)
    val constraintSet = ConstraintSet()
    if (toggle) {
      constraintSet.clone(this, R.layout.activity_constraint_end)
    } else {
      constraintSet.clone(this, R.layout.activity_constraint_start)
    }
    constraintSet.applyTo(root)
    toggle = !toggle
  }


}